package com.yanqu.road.server.logic.recharge.limit.activity;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.mall.MallActivityInfo;
import com.yanqu.road.entity.config.goods.MallInfo;
import com.yanqu.road.entity.config.goods.MallWheelResult;
import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.entity.enums.eSystemId;
import com.yanqu.road.entity.mall.LimitBuyResult;
import com.yanqu.road.entity.servercenter.ServerInfo;
import com.yanqu.road.server.GameServer;
import com.yanqu.road.server.gameplayer.module.cricket.CricketModule;
import com.yanqu.road.server.manger.ServerListMgr;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.activity.MallActivityMgr;
import com.yanqu.road.server.manger.config.SystemOpenMgr;
import com.yanqu.road.server.protocol.GameErrorCode;

public class CricketActivityMallLimit extends BaseActivityMallLimit{
    public CricketActivityMallLimit(long userId, MallInfo mallInfo, ActivityInfo activityInfo) {
        super(userId, mallInfo, activityInfo);
    }

    @Override
    protected int activityCheck() {
        if(!ActivityMgr.activityInTimeAndShowTime(activityInfo)){
            return GameErrorCode.E_ACTIVITY_NO_IN_TIME_CANNOT_BUY;
        }
        if(activityInfo.getType() != eActivityType.CricketSeasonActivity.getValue() && activityInfo.getType() != eActivityType.CricketMallActivity.getValue()){
            return GameErrorCode.E_ACTIVITY_NO_IN_TIME_CANNOT_BUY;
        }
        return 0;
    }

    @Override
    protected boolean isSystemUnLock() {
        return SystemOpenMgr.systemOpen(player, eSystemId.Cricket.getValue());
    }

    @Override
    protected LimitBuyResult buyCountLimit() {
        return activityMallCountLimit(player);
    }

    @Override
    protected int logicCheck() {
        if (!player.getModule(CricketModule.class).canPrecharge(mallInfo.getMallId())) {
            return GameErrorCode.E_MALL_LIMIT_BUY;
        }
        //天数重置限制
        if(mallInfo.getReSetTime() > 0 && !"0".equals(mallInfo.getExtendParam())){
            ServerInfo serverInfo = ServerListMgr.getServerInfo(GameServer.getInstance().getServerId());
            MallWheelResult wheelResult = MallActivityMgr.calWheel(activityInfo, serverInfo);
            MallActivityInfo mallActivityInfo = MallActivityMgr.getInTimeActivity(mallInfo.getActivityId());
            if(mallActivityInfo == null){
                return GameErrorCode.E_MALL_NO_EXIST;
            }
            int allRound = MallActivityMgr.getAllRound(mallInfo.getReSetTime(), mallActivityInfo.getMallInfoMap().values());
            int round = Integer.parseInt(mallInfo.getExtendParam());
            if(round != wheelResult.calRound(allRound, mallInfo.getReSetTime())){
                logger.info("CricketMallActivity activity {} user {} buy mallId {} limit", mallActivityInfo.getActivityId(), player.getUserId(), mallInfo.getMallId());
                return GameErrorCode.E_ACTIVITY_NO_IN_TIME_CANNOT_BUY;
            }
        }
        return 0;
    }
}
